<?php
/**
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * SCWS 分词扩展封装
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @see        http://www.hightman.cn/index.php?scws
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Text_Segment_SCWS extends EGP_Text_Segment_Abstract
{

    /**
     * scws_handle
     *
     * @var resource
     */
    protected $_scws = null;

    /**
     * 分词单词集合
     *
     * @var array
     */
    protected $_words = null;

    /**
     * 构造方法
     *
     * @throws EGP_Exception
     */
    public function __construct()
    {
        if (!EGP_Core::loadExtension('scws')) {
            throw new EGP_Exception('未加载 SCWS 分词扩展');
        }

        $this->_scws = scws_new();
    }

    /**
     * 设定分词词典、规则集、欲分文本字符串的字符集
     *
     * @param  string  $charset
     * @return EGP_Text_Segment_SCWS
     */
    public function setCharset($charset)
    {
        $charset = strtolower($charset);
        if ($charset != 'utf8' && $charset != 'gbk') {
            throw new EGP_Exception("不支付的字符类型：'$charset'");
        }

        $this->_scws->set_charset($charset);

        return $this;
    }

    /**
     * 设定分词所用的词典
     *
     * @param  string  $dict
     * @return EGP_Text_Segment_SCWS
     */
    public function setDict($dict)
    {
        $this->_scws->set_dict($dict);

        return $this;
    }

    /**
     * 设定分词所用的新词识别规则集
     *
     * @param  string  $rule
     * @return EGP_Text_Segment_SCWS
     */
    public function setRule($rule)
    {
        $this->_scws->set_rule($rule);

        return $this;
    }

    /**
     * 设定分词返回结果时是否去除一些特殊的标点符号之类
     *
     * @param  boolean  $ignore
     * @return EGP_Text_Segment_SCWS
     */
    public function setIgnore($ignore)
    {
        $this->_scws->set_ignore((boolean) $ignore);

        return $this;
    }

    /**
     * 设定分词返回结果时是否复式分割
     *
     * @param  boolean  $multi
     * @return EGP_Text_Segment_SCWS
     */
    public function setMulti($multi)
    {
        $this->_scws->set_multi((boolean) $multi);

        return $this;
    }

    /**
     * 设定分词所要切割的文本
     *
     * @param  string  $text
     * @return EGP_Text_Segment_SCWS
     */
    public function setText($text)
    {
        $this->_words = null;

        $search = array_keys($this->_replace);
        $replace = array_values($this->_replace);
        $this->_scws->send_text(str_replace($search, $replace, $text));

        return $this;
    }

    /**
     * 获取分词单词集合
     *
     * @return array
     */
    public function getAllWords()
    {
        if (null == $this->_words) {
            $this->_words = array();
            while ($tmp = $this->_scws->get_result()) {
                foreach ($tmp as $w) {
                    if (!in_array($w['word'], $this->_filter)) {
                        $this->_words[] = $w;
                    }
                }
            }
        }

        return $this->_words;
    }

    /**
     * 获取分词单词集合
     *
     * @return array
     */
    public function getWords()
    {
        $result = array();
        $words = $this->getAllWords();
        foreach ($words as $w) {
            $result[] = $w['word'];
        }

        return $result;
    }

    /**
     * 获取高频词
     *
     * @param  integer  $limit
     * @param  string   $attr
     * @param  integer  $times
     * @return array
     */
    public function getTops($limit = 5, $attr = null, $times = 0)
    {
        $words = empty($attr) ?
            $this->_scws->get_tops(100) :
            $this->_scws->get_tops(100, $attr);

        $tops = array();
        if (is_array($words)) {
            foreach ($words as $word) {
                if ($word['times'] > $times) {
                    $tops[$word['times'] . $word['word']] = $word;
                }
            }
        }

        krsort($tops);
        if (count($tops) > $limit) {
            $tops = array_slice($tops, 0, $limit);
        }

        return array_values($tops);
    }

    /**
     * 获取 SCWS 版本号
     *
     * @return string
     */
    public function getVersion()
    {
        return scws_version();
    }

    /**
     * 析构方法，关闭一个已打开的 scws 分词操作句柄
     */
    public function __destruct()
    {
        $this->_scws->close();
    }

}